#########################################################################################
####################################  TABLE A15   #######################################
####################   Input Parameters and Simulation Results   ########################
#########################################################################################

rm(list = ls())

library(pacman)
p_load(tidyverse, ggplot2, msm, sandwich, here, xtable, readxl)
set.seed(12345)


setwd("C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable_v0_10182023")

#########################################################################################

load(file = "simulations/simulations_output_2.Rda")
load(file = "simulations/simulations_input_params.rda")

# predict discirmination rate from observed X in inverted function
getX <- function(y, model) {
  b <- coef(model)[1]
  c <- coef(model)[2]
  d <- coef(model)[3]
  x <- -(sqrt(-4*b*d+ c^2 + 4*d*y) + c)/(2*d)
  return(x)
}

group_count <- read.csv("figure_5/figure_5_input.csv")
n_femmaj <- group_count$group_N[18]
n_malmaj <- group_count$group_N[13]
task_influence <- read.csv("figure_1/intermediate_data/combined_groupgenderminfl_ratio_ravg_forgraphing.csv")
global_influence <- read.csv("figure_1/intermediate_data/combined_groupfemvotesminfl_avgpre_ratio_forgraphing.csv")
spokesperson <- read.csv("figure_1/intermediate_data/combined_genderspokes_ratio_ravg_forgraphing.csv")


femMajValues  <- c(task_influence$v2[1], global_influence$v2[1], spokesperson$v2[1])
maleMajValues <- c(task_influence$v2[2], global_influence$v2[2], spokesperson$v2[2])

femMajDiscrim <- getX(femMajValues, femaleMajorityTrend)
maleMajDiscrim <- getX(maleMajValues, maleMajorityTrend)

pointsFrame <- as.data.frame(cbind(femMajDiscrim, femMajValues, maleMajDiscrim, maleMajValues))

tab <- matrix(c(" ", " ", " ", modelInputsParams$`Male-majority groups`[1],
                modelInputsParams$`Female-majority groups`[1], "Data", 
                round(modelInputsParams$`Male-majority groups`[2], digits=2), 
                round(modelInputsParams$`Female-majority groups`[2], digits=2), "Data", 
                round(modelInputsParams$`Male-majority groups`[3], digits=2), 
                round(modelInputsParams$`Female-majority groups`[3], digits=2), "Model", 
                ' ',' ', ' ',
                round(pointsFrame$maleMajValues[1], digits=2), 
                round(pointsFrame$femMajValues[1],digits=2), 'Data',
                round(pointsFrame$maleMajDiscrim[1],digits=2), 
                round(pointsFrame$femMajDiscrim[1],digits=2), 'Model', 
                round(pointsFrame$maleMajValues[2], digits=2), 
                round(pointsFrame$femMajValues[2],digits=2), 'Data',
                round(pointsFrame$maleMajDiscrim[2],digits=2), 
                round(pointsFrame$femMajDiscrim[2],digits=2), 'Model',
                round(pointsFrame$maleMajValues[3], digits=2), 
                round(pointsFrame$femMajValues[3],digits=2), 'Data',
                round(pointsFrame$maleMajDiscrim[3],digits=2), 
                round(pointsFrame$femMajDiscrim[3],digits=2), 'Model'), ncol=3, byrow=TRUE)

colnames(tab) <- c('male_majority','female_majority','source')
rownames(tab) <- c('Panel A', modelInputsParams$Measure[1],
                   modelInputsParams$Measure[2], modelInputsParams$Measure[3], 
                   'Panel B', 'Votes per woman, task influence', 
                   'Implied discrimination penalty (in SD)', 
                   'Votes per woman, global influence', 
                   'Implied discrimination penalty (in SD)',
                   'Votes per woman, incentivized spokesperson election', 
                   'Implied discrimination penalty (in SD)')
tab <- as.table(tab)
tab
write.table(tab, file = "table_a15/table_a15.txt", sep = "|",
            row.names = TRUE, col.names = TRUE)
